<?php
/**
 * @todo 公共类库
 * Created by PhpStorm.
 * User: zhangbaohua
 * Date: 2018/7/13
 * Time: 下午1:36
 */
namespace App\Common\lib;

use Illuminate\Support\Facades\Session;

class Util {

    const XBB_CODE = 'code=OO1ncg1,njcly-xx';
    const XBB_URLS = 'http://test.zfx99.com:8012'; // https://zfx99.com
    const BILL_URLS = 'http://test.zfx99.com'; // http://www.zfx99.com
    /**
     * @todo 对内
     */
    public static function verifyMdXbbCode($params)
    {
        $splicing = "";
        ksort($params);
        foreach ($params as $key => $val) {
            $splicing .= $key.'='.$val.'&';
        }
        return strtoupper( md5( $splicing.self::XBB_CODE ) );
    }

    /**
     * 发送数据
     * @param String $url     请求的地址
     * @param Array  $header  自定义的header数据
     * @param Array  $content POST的数据
     * @param  $requestType默认是10=post 20=get get时content给个[]
     * @return String
     */
    public static function toCurl($url, $content, $requestType=10)
    {
        $token = Session::get('dAccessToken');
        $ch = curl_init();
        if (substr($url,0,5) == 'https') {
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 跳过证书检查
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, true);  // 从证书中检查SSL加密算法是否存在
        }
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_URL, self::XBB_URLS.$url);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            'dAccessToken:'.$token
        ));

        if ( $requestType == 10 ) {
            curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($content)); // http_build_query
        }
        $response = curl_exec($ch);


        if ($error=curl_error($ch)) {
            die($error);
        }
        curl_close($ch);
//        $result = json_decode($response, true);
//        if ( $result['code'] == "666" ) {
//            Session::forget('dAccessToken');
//            return redirect('/');
//        }
        return $response;
    }

    /**
     * PHP发送Json对象数据
     *
     * @param $url 请求url
     * @param $jsonStr 发送的json字符串
     * @return array
     */
    public static function http_post_json($url, $jsonStr)
    {
        $token = Session::get('bAccessToken');
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_URL, self::XBB_URLS.$url);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonStr);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
                'bAccessToken:'.$token,
                'Content-Type: application/json; charset=utf-8',
                'Content-Length: ' . strlen($jsonStr)
            )
        );
        $response = curl_exec($ch);
        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        curl_close($ch);
//        $result = json_decode($response, true);
//        if ( $result['code'] == "666" ) {
//            Session::forget('bAccessToken');
//            return redirect('/');
//        }
        return array($httpCode, $response);
    }

    /**
     * PHP发送Json对象数据
     *
     * @param $url 请求url
     * @param $jsonStr 发送的json字符串
     * @return array
     */
    public static function toOneselfApi($url, $jsonStr)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_URL, self::BILL_URLS.$url);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonStr);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
                'Content-Type: application/json; charset=utf-8',
                'Content-Length: ' . strlen($jsonStr)
            )
        );
        $response = curl_exec($ch);
        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        curl_close($ch);

        return array($httpCode, $response);
    }

    /**
     * @todo 提取图片base64
     * @param $file
     * @return string
     */
    public function fileToBase64($file){
        $base64_file = '';
        if(file_exists($file)){
            $mime_type= mime_content_type($file);
            $base64_data = base64_encode(file_get_contents($file));
            $base64_file = 'data:'.$mime_type.';base64,'.$base64_data;
        }
        return $base64_file;
    }

    /**
     * @todo base64保存成图片
     * @param $base64_url
     * @return array
     */
    public function decodeFile($base64_url)
    {
        preg_match('/^data:image\/(\w+);base64/', $base64_url, $out);

        $type       = $out[1];
        $type_param = 'data:image/png' . ';base64,';
        $fileStream = str_replace($type_param, '', $base64_url);
        $fileStream = base64_decode($fileStream);

        return array(
            'type'       => $type,
            'fileStream' => $fileStream
        );

    }
}